using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;
using System.Runtime.Serialization;
using Microsoft.Xna.Framework.Graphics;

namespace Rogue
{
    #region body setup
    public class Frame : BodyPart
    {
        public Frame(Entity a_Owner) : base(a_Owner)
        {
            m_MaxHealth = m_Health = 50;
            m_DrawWhenDead = false;
        }

        public override string DescribeCurrHealth()
        {
            if (m_Health > 25)
                return "dented";
            else if (m_Health > 0)
                return "splintering";
            else
                return "destroyed";
        }
    }

    // What do you call the actual moving part of a door? the door entity's... door?

    public class DoorHinge : BodyPart
    {
        public DoorHinge(BodyPart a_Parent, string a_Loc)
            : base(a_Parent, a_Loc)
        {
            m_DrawWhenDead = false;
            m_Health = 10;
        }

        public override string DescribeCurrHealth()
        {
            if (m_Health >= 10)
                return "pristine";
            if (m_Health > 0)
                return "cracked";
            
            return "broken";
        }
    }
    #endregion

    [Serializable]
    public class Door : Entity
    {
        public Door(string name)
            : base(name)
        {
            m_BodyRoot = new Frame(this);
            new DoorHinge(m_BodyRoot, "top");
            new DoorHinge(m_BodyRoot, "bottom");
            m_BodyRoot.OnSubPartLoss += SubPartLossHandler;

            m_BodyRoot.InitAttackIndices();
        }

        // this is what can kill us.
        void SubPartLossHandler(BodyPart a_Part)
        {
            // no hinges or no frame means dead
            if (!m_BodyRoot.HasParts<DoorHinge>() || m_BodyRoot.GetHealth() == 0)
            {
                m_IsAlive = false;
            }
        }

        public override void PrintDeathMessage()
        {
            if (!m_BodyRoot.HasParts<DoorHinge>())
                ConsoleWindow.Instance.AddMessage("The door falls off its hinges.", Color.Red);
            else
                ConsoleWindow.Instance.AddMessage("The door is broken.", Color.Red);
        }

        #region Serialization
        public Door(SerializationInfo info, StreamingContext context) : base(info, context)
        {
            // Nothing
        }
        #endregion

        static DetailWindowImages s_DoorDamageImages = new DetailWindowImages("door");
        public override DetailWindowImages GetDetailWindowImages()
        {
            return s_DoorDamageImages;
        }
    }

}
